package edu.uta.BillMeApp;

import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import edu.BillME.datapars.HttpConnection;
import android.widget.ListView;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
/* @auther Siddharth Patel & Susan Foster */
public class ShareBill extends ListActivity{
JSONArray jArray;
String result = null;
StringBuilder sb=null;
InputStream is = null;
final static String URL="http://omega.uta.edu/~smp3753/first.php";
@Override
public void onCreate(Bundle savedInstanceState) {
	
   super.onCreate(savedInstanceState);
   Bundle gotBundle=getIntent().getExtras();
   final String getUN = gotBundle.getString("user");
   final String amount=gotBundle.getString("amount");
   final String desc=gotBundle.getString("desc");
   final String date=gotBundle.getString("date");
   final String MF=gotBundle.getString("MF");
   final String flag=gotBundle.getString("flag");
   ArrayAdapter<String> servers = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
  
  
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

postParameters.add(new BasicNameValuePair("firstname", getUN));

   //paring data
   try{
	  
       jArray = new JSONArray(HttpConnection.executeHttpPost(URL,postParameters));
       Log.e("at histrans", "histry trans" );
       for(Integer i=0; i< jArray.length(); i++){
           try{
               //Get My JSONObject and grab the String Value that I want.
               String obj = jArray.getJSONObject(i).getString("FriendName");
               //Add the string to the list
               servers.add(obj);
               
               Log.e("value","value of servers ");
               
               setListAdapter(servers);
               
           }catch(JSONException e){
            Toast.makeText(getBaseContext(), "Not Found" ,Toast.LENGTH_LONG).show();
           }
                             
 
       }

   }catch (Exception e1) {
    Log.e("log_tag", "Error final1212 "+e1.toString());
    }
   
   ListView lv = getListView();
   lv.setTextFilterEnabled(true);
   
   
   lv.setOnItemClickListener(new OnItemClickListener(){

	   public void onItemClick(AdapterView<?> arg0, View arg1, int arg3, long arg4){
		   String friendName=((TextView) arg1).getText().toString();
		  
	   ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("username", getUN));
		postParameters.add(new BasicNameValuePair("friendname", friendName));
		postParameters.add(new BasicNameValuePair("amount", amount));
		postParameters.add(new BasicNameValuePair("date",date));
		postParameters.add(new BasicNameValuePair("monthly",MF));
		postParameters.add(new BasicNameValuePair("description", desc));
		postParameters.add(new BasicNameValuePair("flag", flag));

		String response=null;
		try {
			response = HttpConnection.executeHttpPost("http://omega.uta.edu/~smp3753/sharepayment.php", postParameters);
			Log.e("resonse at  sharebill",response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		String res=response.toString();
		res=res.replace("\n", "");
		
		if(res.equals("1")){	
			if(flag.contains("1")){
				Toast.makeText(getApplicationContext(), "bill submitted" ,Toast.LENGTH_SHORT).show();}
			else{Toast.makeText(getApplicationContext(), "paymet reported" ,Toast.LENGTH_SHORT).show();}

			}
			else{
				Log.d("share","doesn't work");
				Toast.makeText(getApplicationContext(), "not working" ,Toast.LENGTH_LONG).show();
			}


		}//end onClick
});//end setOnClickListener*/
  
}}